package cn.cqows.controller;

import cn.cqows.core.SerialPortCore;
import cn.cqows.pojo.FaultQue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/fault")
public class FaultController {


    public static List<FaultQue> lists = new ArrayList<>();

    @PostConstruct
    public void init()throws Exception {
        InputStream inputStream = FaultController.class.getClassLoader().getResourceAsStream("a.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
        String redline;
        while ((redline=reader.readLine())!=null){
            String[] split = redline.split("-");
            FaultQue fault = new FaultQue();
            fault.setId(Integer.valueOf(split[0]));
            fault.setQueName(split[1]);
            fault.setSett(split[2]);
            fault.setFlag(false);
            lists.add(fault);
        }
        reader.close();
    }
    @Autowired
    private SerialPortCore serialPortCore;

    @GetMapping
    public HashMap getFaultList(int currentPage, int pageSize) {
        ArrayList<FaultQue> faultPage = new ArrayList<>();
        int start;
        int end;

        if(currentPage*pageSize<lists.size()){
            if (currentPage==1){
                start=0;
                end=pageSize;
            }else {
                start=(currentPage-1)*pageSize;
                end=currentPage*pageSize;
            }
        }else {
            start=(currentPage-1)*pageSize;
            end=lists.size();
        }
        while (true){

            faultPage.add(lists.get(start));
            start++;
            if (start>=end){
                break;
            }
        }

//        Page<FaultQue> faultPage = faultmapper.selectPage(page, null);
//        this.items = res.data.records;
//        this.pagination.pageSize = res.data.size;
//        this.pagination.current = res.data.current;
//        this.pagination.total = res.data.total;
        HashMap hashMap = new HashMap();
        hashMap.put("records",faultPage);
        hashMap.put("current",currentPage);
        hashMap.put("total",96);
        hashMap.put("size",pageSize);
        return hashMap;
    }

    @PostMapping
    public void settingFault(@RequestBody int[] faults) {

        for (int fault : faults) {
//            FaultQue fault1 = faultmapper.selectById(fault);
            FaultQue fault1 = lists.get(fault-1);
//            System.out.println(fault1);
            fault1.setFlag(true);
            serialPortCore.sendMsg(zhuanhuan(fault1.getSett()));
        }
    }

    //重置
    @PostMapping("/reset")
    public void reset() {
        String msgStr = "55 01 33 ff ff ff ff 85";
        serialPortCore.sendMsg(zhuanhuan(msgStr));
        String msgSt2 = "55 02 33 ff ff ff ff 86";
        serialPortCore.sendMsg(zhuanhuan(msgSt2));
        String msgSt3 = "55 03 33 ff ff ff ff 87";
        serialPortCore.sendMsg(zhuanhuan(msgSt3));
        lists.forEach( list->{
            list.setFlag(false);
        });
    }


    public byte[] zhuanhuan(String msgStr) {
        String[] msgStrSZ = msgStr.split(" ");
        byte[] bytes = new byte[msgStrSZ.length];
        for (int i = 0; i < msgStrSZ.length; i++) {
            bytes[i] = (byte) Integer.parseInt(msgStrSZ[i], 16);
        }
        return bytes;
    }
}
